'use strict';

const semver = require('semver');
const colors = require('colors/safe');
const log = require('@test-hy-in/log');
const LOWEST_NODE_VERSION = '12.0.0';

class Command {
    constructor(argv){
        if(!argv){
            throw new Error('参数不能空！')
        };
        if(!Array.isArray(argv)){
            throw new Error('参数必须为数组！')
        };
        if(argv.lentgh<1){
            throw new Error('参数列表不能为空！')
        };
        this._argv = argv;
        let runner = new Promise((resolve,reject)=>{
            let chain = Promise.resolve();
            chain = chain.then(()=>this.checkNodeVersion());
            chain = chain.then(()=>this.initArgs());
            chain = chain.then(()=>this.init());
            chain = chain.then(()=>this.exec());
            chain.catch(err=>{
                log.error(err.message)
            })
        })
    }
    initArgs(){
        // console.log('command类-initArgs')
        this._cmd = this._argv[this._argv.length-1];
        this._argv = this._argv.slice(0,this._argv.length-1);
    };
    // 检查node版本号
    checkNodeVersion(){
        // console.log('command类-checkNodeVersion')
        const currentVersion = process.version;
        const lowestNodeVersion = LOWEST_NODE_VERSION;
        if(!semver.gte(currentVersion,lowestNodeVersion)){
            throw new Error(colors.red(`test-hy-in需要安装v${lowestNodeVersion}以上版本的node.js`))
        }
    }
    init(){
        // console.log('command类-init')
        throw new Error('init必须实现！')
    };
    exec(){
        // console.log('command类-exec')
        throw new Error('exec必须实现！')
    }
}

module.exports = Command;